/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import controller.ProfissionalController;
import exception.DuplicateConstraintException;
import exception.RemoveConstraintException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import model.entidades.Profissional;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.Conversation;
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ConversationScoped;

/**
 *
 * @author helton
 */
@Named
@ConversationScoped
public class ProfissionalView implements View, Serializable {

    @Inject
    ProfissionalController controller;
    @Inject
    Conversation conversation;
    private Profissional profissional;
    private List<Profissional> profissionais;
    private Map<String, String> sexo = new HashMap<>();
    private Map<String, String> estCiv = new HashMap<>();

    @Override
    @PostConstruct
    public void init() {
        profissional = new Profissional();
    }

    @Override
    public void closeConversation(ActionEvent event) {
        conversation.close();
    }

    @Override
    public String actionNovo() {
        profissional = new Profissional();
        return "cadastrarProfis";
    }

    @Override
    public String actionSalvar() throws DuplicateConstraintException {
        profissional.setCpf(profissional.getCpf().replaceAll("-", "").replaceAll("\\.", "").trim());
        profissional.setCepEnd(profissional.getCepEnd().replaceAll("-", "").trim());
        profissional.setCns(profissional.getCns().replaceAll("\\.", "").trim());
        System.out.println("CPF"+profissional.getCpf());
        if (profissional.getId() == null) {
            controller.insert(profissional);
        } else {
            controller.update(profissional);
        }
        profissionais = null;
        return "formacoes";
    }
    
    public String voltar(){
        return "dadosPessoal";
    }

    @Override
    public String actionListar() {
        return null;
    }

    @Override
    public void actionRemover() throws RemoveConstraintException {
    }

    public String validarDados() {
        return "dadosContato";
    }

    public Map<String, String> getSexo() {
        sexo.put("Masculino", "M");
        sexo.put("Feminino", "F");
        return sexo;
    }

    public void setSexo(Map<String, String> sexo) {
        this.sexo = sexo;
    }

    public Map<String, String> getEstCiv() {
        estCiv.put("Solteiro", "Solteiro");
        estCiv.put("Casado", "Casado");
        estCiv.put("Viúvo", "Viúvo");
        estCiv.put("Separado judicialmente", "Separado judicialmente");
        estCiv.put("Divorciado", "Divorciado");
        return estCiv;
    }

    public void setEstCiv(Map<String, String> estCiv) {
        this.estCiv = estCiv;
    }

    public Profissional getProfissional() {
        return profissional;
    }

    public void setProfissional(Profissional profissional) {
        this.profissional = profissional;
    }

    public List<Profissional> getProfissionais() {
        if (profissionais == null) {
            profissionais = controller.getAll();
        }
        return profissionais;
    }

    public void setProfissionais(List<Profissional> profissionais) {
        this.profissionais = profissionais;
    }
}
